package com.web.daoimpl;

import com.web.dao.BaseDao;
import com.web.util.DatabaseUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * basedao接口的实现类
 *
 * @author 12270
 */
public class BaseDaoImpl implements BaseDao {

    

    @Override
    public int update(String sql, int[] i, String... params) {
        Connection con = null;
        PreparedStatement ps = null;
        int rows = 0; //受影响的行数
        int j = 0;
        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement(sql);
            for (j = 0; j < params.length; j++) {
                ps.setObject(j + 1, params[j]);
            }
            for (int k = 0; k < i.length; k++) {
                ps.setInt(j + 1, i[k]);
                j++;
            }
            rows = ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(null, ps, con);

        }
        return rows;
    }

    @Override
    public int update(String sql, int[] i) {
        Connection con = null;
        PreparedStatement ps = null;
        int rows = 0; //受影响的行数
        try {
            con=DatabaseUtil.getConnection();
            ps=con.prepareStatement(sql);
            for(int j=0;j<i.length;j++){
                ps.setObject(j+1, i[j]);
            }
            rows=ps.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(BaseDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
            DatabaseUtil.close(null, ps, con);
        }
        return rows;
    }

    @Override
    public int excuteUpdate_o(String sql, Object... params) {
        Connection con = null;
        PreparedStatement ps = null;
        int rows = 0; //受影响的行数

        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                ps.setObject(i + 1, params[i]);
            }
            rows = ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(null, ps, con);

        }
        return rows;
    }

    @Override
    public int excuteUpdate(String sql, String... params) {
        Connection con = null;
        PreparedStatement ps = null;
        int rows = 0; //受影响的行数

        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                ps.setObject(i + 1, params[i]);
            }
            rows = ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(null, ps, con);

        }
        return rows;
    }

}
